package com.q.tqb.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.grab.RedPersonDataActivity;
import com.q.tqb.activity.mine.MineRedEnvelopeActivity;
import com.q.tqb.adapter.RankAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.model.RankInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.refresh.GRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class RankFragment extends Fragment {

    @BindView(R.id.tv_all_rank)
    TextView mTvAllRank;
    @BindView(R.id.tv_friend_rank)
    TextView mTvFriendRank;
    @BindView(R.id.rv_rank)
    GRecyclerView mRvRank;
    Unbinder unbinder;
    private int rankType;

    private RankAdapter mRankAdapter;
    private ArrayList<RankInfo> mGrabRedDatas;
    private int currentPageNo;
    private String url;


    public RankFragment() {
        // Required empty public constructor
    }

    public static RankFragment newInstance(int rankType) {
        RankFragment fragment = new RankFragment();
        Bundle args = new Bundle();
        args.putInt("rankType", rankType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            rankType = getArguments().getInt("rankType");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rank, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        mGrabRedDatas = new ArrayList<>();
        if (rankType == 0) {
            url = AppConst.URL_USER_SEND;
        } else {
            url = AppConst.URL_USER_GET;
        }

        mTvAllRank.setSelected(true);
        mTvFriendRank.setSelected(false);

        mRvRank.setLoadingMoreEnabled(true);
        mRvRank.setLoadingListener(new GRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPageNo = 1;
                mRvRank.setNoMore(false);
                mRvRank.setLoadingMoreEnabled(true);
                getListData();
            }

            @Override
            public void onLoadMore() {
                currentPageNo++;
                getListData();
            }
        });

        mRankAdapter = new RankAdapter(getActivity(), mGrabRedDatas, rankType);
        mRankAdapter.setOnItemClickListener(new RankAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
          //      Intent intent = new Intent(getActivity(), MineRedEnvelopeActivity.class);
                Intent intent = new Intent(getActivity(), RedPersonDataActivity.class);
                intent.putExtra("uid", mGrabRedDatas.get(position).getUid());
                startActivity(intent);
            }
        });
        LinearLayoutManager linearLayoutManagert = new LinearLayoutManager(getActivity());
        mRvRank.setLayoutManager(linearLayoutManagert);
        mRvRank.setAdapter(mRankAdapter);
        mRvRank.setRefresh();
    }


    @OnClick({R.id.tv_all_rank, R.id.tv_friend_rank})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_all_rank:
                mTvAllRank.setSelected(true);
                mTvFriendRank.setSelected(false);
                break;
            case R.id.tv_friend_rank:
                mTvFriendRank.setSelected(true);
                mTvAllRank.setSelected(false);
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNo", currentPageNo + "");
        map.put("pageSize", 10 + "");
        OkHttpUtil.getInstance().post(getActivity(), url, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                ArrayList<RankInfo> grabRedDatas = gson.fromJson(element, new TypeToken<ArrayList<RankInfo>>() {
                }.getType());
                if (currentPageNo == 1) {
                    mGrabRedDatas.clear();
                    mRvRank.refreshComplete();
                    if (grabRedDatas.size() < 10) {
                        mRvRank.setNoMore(true);
                    }
                } else {
                    if (grabRedDatas.size() < 10) {
                        mRvRank.setNoMore(true);
                    } else {
                        mRvRank.loadMoreComplete();
                    }
                }
                mGrabRedDatas.addAll(grabRedDatas);
                mRankAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                if (currentPageNo == 1) {
                    mRvRank.refreshComplete();
                } else {
                    mRvRank.loadMoreComplete();
                }
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

}
